/**
 * 〈〉
 *
 * @author 王青杰
 * @create 2018-03-2510:24
 * @since 1.0.0
 */
package com.example.service.impl;

import com.example.dao.UserDao;
import com.example.entity.TbUser;
import com.example.enums.ResultEnum;
import com.example.service.UserService;
import com.example.util.Md5Utils;
import com.example.util.ResultMsg;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    UserDao userDao;

    @Override
    public ResultMsg insert(TbUser user){
        if(user.getId()!=null){
            if(userDao.findById(user.getId())!=null){
                return null;
            }
        }
        user.setPassword(Md5Utils.getMD5(user.getPassword()));
        userDao.save(user);
        ResultMsg resultMsg = new ResultMsg(ResultEnum.SAVE);
        resultMsg.setData(user);
        return resultMsg;
    }

    @Override
    public ResultMsg delete(int userId) {
        if(userDao.getOne(userId)!=null){
            userDao.deleteById(userId);
            return new ResultMsg(ResultEnum.DELETE);
        }
        return new ResultMsg(ResultEnum.UNDELETE);
    }
}
